#include<iostream>
#include<vector>
#include<string>

using namespace std;


string longestCommonPrefix(vector<string>& strs) {
    if (strs.empty()) return "";
    if (strs.size() < 2) return strs[0];
    // 方法一：两两比较
    //先求出前两个字串的公共前缀，
    string PublicPrefix;
    int n = strs.size();
    int i = 0;
    while (true) {
        if (strs[0][i] == strs[1][i]) {
            PublicPrefix += strs[0][i];
            i++;
        }
        else break;
        if (i > strs[0].size() || i > strs[1].size()) break;
    }
    if (PublicPrefix.empty()) return"";
    //再用求出的公共前缀与后续的子串比较
    for (int i = 2; i < n; i++)
    {
        string str = PublicPrefix;
        for (int j = 0; j < strs[i].size(); ++j)
        {
            if (str[j] != strs[i][j])
            {
                PublicPrefix = str.substr(0, j + 1);
                break;
            }
        }
    }
    return PublicPrefix;
}
int main()
{
    vector<string> strs = { "abcc","abcc","abcb" };
    longestCommonPrefix(strs);
	return 0;
}